/**
 * ============================================================================
 *
 * Copyright (C) 2013 Android SCasia Systems.  All rights reserved. The content 
 * presented herein may not, under any circumstances, be reproduced, in 
 * whole or in any part or form, without written permission from 
 * SCasia Systems.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are NOT permitted. Neither the name of SCasia Systems,
 * nor the names of contributors may be used to endorse or promote products 
 * derived from this software without specific prior written permission.
 *
 * ============================================================================
 *
 * Author: Kelvin
 *  
 *
 * Revision History
 * ----------------------------------------------------------------------------
 * Date                Author              Comment, bug number, fix description
 *
 * January 20, 2013      Kelvin@futureworkz.com           version 1.0
 *
 * ----------------------------------------------------------------------------
 */
package cungmua.vn.kiosk;

import cungmua.vn.kiosk.utils.Utils;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;

// TODO: Auto-generated Javadoc
/**
 * The Class AbstractActivity.
 * 
 * @author Kelvin
 */
public class AbstractActivity extends Activity {

	/** The self. */
	public AbstractActivity self;
	/** The tag. */
	public static String TAG = "";

	/** The Constant GONE. */
	public static final int GONE = View.GONE;

	/** The Constant VISIBLE. */
	public static final int VISIBLE = View.VISIBLE;

	/** The Constant INVISIBLE. */
	public static final int INVISIBLE = View.INVISIBLE;
	public static final String DONHANG = "donhang";
	public static final String ORDERID="orderId";
	public static final String STATUS="status";

	public static final int DH_STATUS_RESTORE=10; 
	public static final int DH_STATUS_SUCCESS=7; 
	public static final int DH_STATUS_DELAY=1;
	public static final int DH_STATUS_CONTACT_FAILED=0;
	public static final int DH_STATUS_WRONG_PRODUCT=3;
	public static final int DH_STATUS_CANCEL=2;
	
	public static final int SKU_STATUS_SUCCESS=1;
	public static final int SKU_STATUS_CANCEL=0;
	
	public static final int SKU_TYPE_ALL=0;
	public static final int SKU_TYPE_PRODUCT=1;
	public static final int SKU_TYPE_VOUCHER=2;
	
//	=========== why cancel=======
	public static final int REASON_KHACHHUYDODOIY=9;
	public static final int REASON_KHACHHUYDODATTHU=12;
	public static final int REASON_KHACHHUYDOHANGKHONGPHUHOP=10;
	public static final int REASON_KHACHHUYDOHANGKHONGDATCHATLUONG=8;
	public static final int REASON_KHACHHUYDOKHONGTINTUONG=14;
	public static final int REASON_KHACHHUYDOVUONGTHANHTOAN=11;
	public static final int REASON_KHACHHUYDOGIAOHANGCHAM=13;
	public static final int REASON_KHACHLENCONGTYNHAN=4;
	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Utils.requestWindowFeature(this);
		this.getWindow().getAttributes().windowAnimations = R.style.Animations_Activity;

		self = this;

	}

	/**
	 * Checks if is empty.
	 * 
	 * @param str
	 *            the str
	 * @return true, if is empty
	 */
	public boolean isEmpty(String str) {
		return TextUtils.isEmpty(str);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onResume()
	 */
	@Override
	protected void onResume() {
		super.onResume();
	}

	/**
	 * Check network connection.
	 * 
	 * @return true, if is network available
	 */
	public boolean isNetworkAvailable() {
		ConnectivityManager conMgr = (ConnectivityManager) self
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo i = conMgr.getActiveNetworkInfo();
		if (i == null) {
			return false;
		}
		if (!i.isConnected()) {
			return false;
		}
		if (!i.isAvailable()) {
			return false;
		}
		return true;
	}

	/**
	 * Go to other activity.
	 * 
	 * @param context
	 *            the context
	 * @param cla
	 *            the cla
	 */
	protected void gotoActivity(Context context, Class<?> cla) {
		Intent intent = new Intent(context, cla);
		startActivity(intent);
	}

	/**
	 * Goto activity.
	 * 
	 * @param context
	 *            the context
	 * @param cla
	 *            the cla
	 * @param flag
	 *            the flag
	 */
	protected void gotoActivity(Context context, Class<?> cla, int flag) {
		Intent intent = new Intent(context, cla);
		intent.setFlags(flag);
		startActivity(intent);
	}

	/**
	 * Goto activity with bundle.
	 * 
	 * @param context
	 *            the context
	 * @param cla
	 *            the cla
	 * @param bundle
	 *            the bundle
	 */
	protected void gotoActivity(Context context, Class<?> cla, Bundle bundle) {
		Intent intent = new Intent(context, cla);
		intent.putExtras(bundle);
		startActivity(intent);
	}

	/**
	 * Goto activity for result.
	 * 
	 * @param context
	 *            the context
	 * @param cla
	 *            the cla
	 * @param reqCode
	 *            the req code
	 */
	protected void gotoActivityForResult(Context context, Class<?> cla,
			int reqCode) {
		Intent intent = new Intent(context, cla);
		startActivityForResult(intent, reqCode);

	}

	/**
	 * Goto activity.
	 * 
	 * @param context
	 *            the context
	 * @param cla
	 *            the cla
	 * @param flag
	 *            the flag
	 * @param bundle
	 *            the bundle
	 */
	protected void gotoActivity(Context context, Class<?> cla, int flag,
			Bundle bundle) {
		Intent intent = new Intent(context, cla);
		intent.setFlags(flag);
		intent.putExtras(bundle);
		startActivity(intent);
	}

	// ======================= PROGRESS DIALOG ======================

	// ======================= KEYBOARD MANAGER =======================

	/**
	 * Show soft keyboard.
	 * 
	 * @param editText
	 *            the edit text
	 */
	protected void showKeyboard(EditText editText) {
		InputMethodManager inputMethodManager = (InputMethodManager) this
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		if (inputMethodManager != null) {
			inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED,
					0);
		}
	}

	/**
	 * Hide soft keyboard.
	 * 
	 * @param editText
	 *            the edit text
	 */
	protected void hideKeyboard(EditText editText) {
		((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE))
				.hideSoftInputFromWindow(editText.getWindowToken(), 0);
	}

	// =======================TOAST MANAGER===============================

	/**
	 * Show toast.
	 * 
	 * @param msg
	 *            the msg
	 * @param duration
	 *            the duration
	 */
	protected void showToast(String msg, int duration) {
		Toast.makeText(this, msg, duration).show();
	}

	/**
	 * Show toast.
	 * 
	 * @param resId
	 *            the res id
	 * @param time
	 *            the time
	 */
	protected void showToast(int resId, int time) {
		Toast.makeText(this, resId, time).show();
	}

	/**
	 * Show toast.
	 * 
	 * @param view
	 *            the view
	 * @param time
	 *            the time
	 */
	protected void showToast(View view, int time) {
		Toast toast = new Toast(this);
		toast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
		toast.setView(view);
		toast.show();

	}

	// ======================= DIALOG MANAGER =======================

	/**
	 * Show confirm dialog.
	 * 
	 * @param messageId
	 *            the message id
	 * @param positiveLabelId
	 *            the positive label id
	 * @param negativeLabelId
	 *            the negative label id
	 * @param positiveOnClick
	 *            the positive on click
	 * @param negativeOnClick
	 *            the negative on click
	 */
	protected void showDialog(int messageId, int positiveLabelId,
			int negativeLabelId,
			DialogInterface.OnClickListener positiveOnClick,
			DialogInterface.OnClickListener negativeOnClick) {

		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(getString(R.string.app_name));
		builder.setMessage(getString(messageId));
		builder.setPositiveButton(getString(positiveLabelId), positiveOnClick);
		builder.setNegativeButton(getString(negativeLabelId), negativeOnClick);
		AlertDialog alert = builder.create();
		alert.show();
	}

	/**
	 * Show confirm dialog.
	 * 
	 * @param title
	 *            the title
	 * @param messageId
	 *            the message id
	 * @param positiveLabelId
	 *            the positive label id
	 * @param negativeLabelId
	 *            the negative label id
	 * @param positiveOnClick
	 *            the positive on click
	 * @param negativeOnClick
	 *            the negative on click
	 */
	protected void showDialog(String title, int messageId, int positiveLabelId,
			int negativeLabelId,
			DialogInterface.OnClickListener positiveOnClick,
			DialogInterface.OnClickListener negativeOnClick) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(title);
		builder.setMessage(getString(messageId));
		builder.setPositiveButton(getString(positiveLabelId), positiveOnClick);
		builder.setNegativeButton(getString(negativeLabelId), negativeOnClick);
		AlertDialog alert = builder.create();
		alert.show();
	}

	/**
	 * Show information dialog.
	 * 
	 * @param messageId
	 *            the message id
	 * @param positiveLabelId
	 *            the positive label id
	 * @param positiveOnClick
	 *            the positive on click
	 */
	protected void showDialog(int messageId, int positiveLabelId,
			DialogInterface.OnClickListener positiveOnClick) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(getString(R.string.app_name));
		builder.setMessage(getString(messageId));
		builder.setPositiveButton(getString(positiveLabelId), positiveOnClick);
		AlertDialog alert = builder.create();
		alert.show();
	}

	/**
	 * Show delete message dialog.
	 * 
	 * @param positiveOnClick
	 *            the positive on click
	 * @param negativeOnClick
	 *            the negative on click
	 * @param idMessage
	 *            the id message
	 */
	protected void showDeleteDialog(
			DialogInterface.OnClickListener positiveOnClick,
			DialogInterface.OnClickListener negativeOnClick, int idMessage) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(getString(idMessage));
		builder.setPositiveButton(getString(android.R.string.yes),
				positiveOnClick);
		builder.setNegativeButton(getString(android.R.string.no),
				negativeOnClick);
		AlertDialog alert = builder.create();
		alert.show();
	}

	/**
	 * Show single Dialog.
	 * 
	 * @param resId
	 *            resId
	 */
	protected void showSingleDialog(int resId) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(getString(resId));
		builder.setPositiveButton(android.R.string.ok, null);
		AlertDialog dialog = builder.create();
		dialog.show();
	}

	public static AlertDialog getAlertDialog(Activity activity,String title, String message) {

		AlertDialog alertDialog = new AlertDialog.Builder(activity).create();
		alertDialog.setTitle(title);
		alertDialog.setMessage(message);

		alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		return alertDialog;
	}

}
